import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../../models/information.dart';
import '../../models/banner.dart';
import '../../services/auth_service.dart';
import '../../services/information_service.dart';
import '../../services/banner_service.dart';

class InformationLogic extends GetxController {
  final AuthService _authService = AuthService();
  final InformationService _informationService = InformationService();
  final BannerService _bannerService = BannerService();

  final RxList<Information> informationList = <Information>[].obs;
  final RxList<BannerItem> bannerList = <BannerItem>[].obs;
  final isLoading = true.obs;
  final isBannersLoading = true.obs;
  final isAdmin = false.obs;

  @override
  void onInit() {
    super.onInit();
    _checkIfAdmin();
    loadData();
  }

  Future<void> loadData() async {
    await loadInformation();
    await loadBanners();
  }

  Future<void> _checkIfAdmin() async {
    isAdmin.value = await _authService.isCurrentUserAdmin();
  }

  Future<void> loadInformation() async {
    isLoading.value = true;
    try {
      final list = await _informationService.getAllInformation();
      informationList.value = list;
    } catch (e) {
      SmartDialog.showToast('加载信息失败: $e');
    } finally {
      isLoading.value = false;
    }
  }

  Future<void> loadBanners() async {
    isBannersLoading.value = true;
    try {
      final banners = await _bannerService.getAllBanners();

      if (banners.isNotEmpty) {
        bannerList.clear();
        bannerList.addAll(banners);
      } else {
        print('轮播图列表为空');
      }
    } catch (e) {
      print('加载轮播图错误: $e');
      SmartDialog.showToast('加载轮播图失败: $e');
    } finally {
      isBannersLoading.value = false;
    }
  }

  void goToAddInformation() {
    Get.toNamed('/add-information')?.then((_) => loadInformation());
  }

  void goToManageBanners() {
    Get.toNamed('/manage-banners')?.then((_) => loadBanners());
  }

  void goToInformationDetail(Information information) {
    Get.toNamed('/information-detail', arguments: information);
  }

  Future<void> deleteInformation(Information information) async {
    final confirmed = await Get.dialog<bool>(
      AlertDialog(
        title: Text('确认删除'),
        content: Text('确定要删除这条信息吗？此操作不可撤销。'),
        actions: [
          TextButton(
            onPressed: () => Get.back(result: false),
            child: Text('取消'),
          ),
          TextButton(
            onPressed: () => Get.back(result: true),
            child: Text('删除', style: TextStyle(color: Colors.red)),
          ),
        ],
      ),
    );

    if (confirmed == true && information.id != null) {
      try {
        SmartDialog.showLoading(msg: '正在删除...');
        await _informationService.deleteInformation(information.id!);
        SmartDialog.dismiss();
        SmartDialog.showToast('删除成功');
        loadInformation();
      } catch (e) {
        SmartDialog.dismiss();
        SmartDialog.showToast('删除失败: $e');
      }
    }
  }
}